<template>
  <div>
    <el-form :model="form" ref="CateAdd" :rules="rules">
      <el-form-item label="商品分类" prop="cateId">
        <el-select placeholder="顶层分类" v-model="form.id">
          <el-option v-for="item in cate" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="form.name" style="width:300px margin-top:10px ;"></el-input>
        </el-form-item>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDia()">取 消</el-button>
      <el-button type="primary" @click="goAdd(form)">确 定</el-button>
    </div>
  </div>
</template>

<script>
import { goodCate } from '@/api'
export default {
  props: {
    closeDia: {
      type: Function,
      required: true
    }
  },
  data() {
    return {
      form: {
        name: '',
        id: '顶层分类',
        Pid: ''
      },
      cate: [],
      // 验证规则
      rules: {
        name: [
          {
            required: true,
            // 验证不通过时，错误提示文案
            message: '当前字段是必填的'
          }
        ]
      }
    }
  },
  methods: {
    // 获取全部分类
    goodCate() {
      goodCate().then(res => {
        if (res.data.code == 200) {
          const top = {
            Pid: 0,
            id: -1,
            name: '顶层分类'
          }
          this.cate = [...res.data.list, top]
        }
      })
    },
    // 新增分类的表格发送
    goAdd(CateData) {
      this.$refs.CateAdd.validate((valid) => {
        if (valid) {
          // 验证通过
          this.$emit('goAdd', CateData)
        } else {
          // 验证不通过
          this.$message({
            type: 'error',
            message: '表单验证不通过'
          })
        }
      })
    }
  },
  created() {
    this.goodCate()
  }

}
</script>

<style lang="scss" scoped>

</style>
